$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        iRHId:null,
        init:function () {
            this.iRHId = xhTool.getQuery('iRHId');
            this.dataGrid = new xhDataGrid($('#list'), 'lm/rent/getPay/'+this.iRHId);
            this.dataGrid.init().parseToolbar().activeButton(['bt_list_del'], ['bt_list_reimburse']);
            this.listener();
            //绑定dataGrid监听事件
            this.funObj().bindDataGridEvent();
        },
        listener:function () {
            var $this = this;

            //新增事件按钮
            $('#bt_list_add').on('click',function () {
                $this.funObj().openAddForm();
            });

            //删除
            $('#bt_list_del').on('click',function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().askDelQuestion();
            });

            $('#bt_list_reimburse').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().reimburse();
            });

        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        window.top.xhTop.showModal({
                            title: '编辑支付信息',
                            content: '<iframe src="' + baseUrl + 'lm/rent/payForm/' + $this.iRHId + '/'+rowData['iRPId']+' " style="height: 300px;border-style: none;padding: 0px;width: 100%"></iframe>',
                            button: '提交',
                            backClose: false
                        });
                    };
                    $this.dataGrid.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;">';
                            if (row['bReimburse'] == 1) {
                                var content='<div><ul class=" m-top-sm clearfix " style="list-style: none">';
                                var array = row['mReimburse']['file'];
                                if (array && array.length > 0){
                                    for(var i=0;i< array.length;i++){
                                        content+=' <li style="float: left;margin-right:5px; "><table ><tbody><tr><td style="border: 0px;"><div title="'+array[i]['FileName']+'"><a target="_blank" href="'+array[i]['url']+'" style="text-align: center;width: 30px;margin-bottom: 10px;" >' ;
                                        content+= '<i class="fa fa-file-o fa-2x customer-size"   aria-hidden="true"></i>';
                                        content+= '</a></div></td></tr>';
                                        content+= '</tbody></table></li>';
                                    }
                                    content+=  '</ul></div>';
                                }
                                str += '<table >' +
                                            '<tr>' +
                                                '<th colspan="2" style="text-align: center">报销详情</th>' +
                                            '</tr>'+
                                            '<tr>' +
                                                '<td style="width: 75px;">报销备注</td>' +
                                                '<td style="width: 400px;">'
                                                    + row['mReimburse']['content'] +
                                                '</td>' +
                                            '</tr>'+
                                            '<tr>' +
                                                '<td style="width: 75px;">附件</td>' +
                                                '<td style="width: 400px;">'
                                                    + content +
                                                '</td>' +
                                            '</tr>'+
                                       '</table>';
                            }
                            str+='</div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:200,
                                width:780,
                                border:false,
                                cache:false,
                                onLoad:function(){
                                    $this.dataGrid.command('fixDetailRowHeight',index);
                                    $this.dataGrid.command('fixDetailRowWidth',index);
                                }
                            });
                        }
                    });
                    $this.dataGrid.onLoadSuccess = function (data) {
                        $.each(data['rows'],function (index,row) {
                            if(row['bReimburse'] == 0){
                                $this.dataGrid.command('getExpander',index).parent().hide().after('<div style="width: 24px;"></div>');
                            }
                        })
                    }
                },
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '新建支付信息',
                        content: '<iframe src="' + baseUrl + 'lm/rent/payForm/' + $this.iRHId + ' " style="height: 300px;border-style: none;padding: 0px;width: 100%"></iframe>',
                        button: '提交',
                        backClose: false
                    });
                },
                askDelQuestion:function () {
                    var ids = $this.dataGrid.getSelectValues('iRPId');
                    xhExtra.askChangeStatus('lm/rent/delPay',{ids:ids},null,'确定要删除吗',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.dataGrid.reload();
                    })
                },
                reimburse:function () {
                    var id = $this.dataGrid.getSelectValue('iRPId');
                    if ($this.dataGrid.getSelectValue('bReimburse') == 1) {
                        window.top.xhTop.showError('错误','已经报销请勿重复报销');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title: '报销确认',
                        content: '<iframe src="' + baseUrl + 'lm/rent/reimburse?id='+id+'" style="height: 300px;border-style: none;padding: 0px;width: 100%"></iframe>',
                        button: '提交',
                        backClose: false
                    });
                }
            }
        }
    }
};